package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private MovementsMQService movementsMQService;

    @Reference
    private PublishApi publishApi;


    /**
     * 接口名称：动态-点赞
     *
     * @param id 动态id
     */
    public ResponseEntity<Object> likeComment(String id) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(id));
        comment.setCommentType(1);
        comment.setPubType(1);
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());

        // 根据动态id查询动态信息
        Publish publish = publishApi.findById(id);
        // 设置动态发布者的id
        comment.setPublishUserId(publish.getUserId());
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());

        // 2.调用服务提供者api保存评论、修改点赞数量、返回点赞数量
        long count = commentApi.save(comment);

        // 3.保存点赞标记到redis中
        String key = "public_like_comment_" + UserHolder.getUserId() + "_" + id;
        redisTemplate.opsForValue().set(key, "xxx");

        // 4.发送评分消息给MQ
        movementsMQService.sendMsg(id, 3);

        return ResponseEntity.ok(count);
    }

    /**
     * 接口名称：动态-取消点赞
     */
    public ResponseEntity<Object> dislikeComment(String id) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(id));
        comment.setCommentType(1);
        comment.setPubType(1);
        comment.setUserId(UserHolder.getUserId());

        // 2.调用服务提供者api删除评论、修改点赞数量、返回点赞数量
        long count = commentApi.delete(comment);

        // 3.删除点赞标记
        String key = "public_like_comment_" + UserHolder.getUserId() + "_" + id;
        redisTemplate.delete(key);

        return ResponseEntity.ok(count);
    }

    /**
     * 接口名称：动态-喜欢
     */
    public ResponseEntity<Object> loveComment(String id) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(id));
        comment.setCommentType(3);
        comment.setPubType(1);
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());

        Publish byId = publishApi.findById(id);
        comment.setPublishUserId(byId.getUserId());
        // 2.调用服务提供者api保存评论、修改喜欢数量、返回喜欢数量
        long count = commentApi.save(comment);

        // 3.保存喜欢标记到redis中
        String key = "public_love_comment_" + UserHolder.getUserId() + "_" + id;
        redisTemplate.opsForValue().set(key, "xxx");

        return ResponseEntity.ok(count);
    }

    /**
     * 接口名称：动态-取消喜欢
     */
    public ResponseEntity<Object> unloveComment(String id) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(id));
        comment.setCommentType(3);
        comment.setPubType(1);
        comment.setUserId(UserHolder.getUserId());

        // 2.调用服务提供者api删除评论、修改喜欢数量、返回喜欢数量
        long count = commentApi.delete(comment);

        // 3.删除喜欢标记
        String key = "public_love_comment_" + UserHolder.getUserId() + "_" + id;
        redisTemplate.delete(key);

        return ResponseEntity.ok(count);
    }

    /**
     * 接口名称：评论列表
     */
    public ResponseEntity<Object> queryCommentsList(Integer page, Integer pagesize, String movementId) {
        // 1.调用服务提供者api分页查询评论列表
        PageResult pageResult = commentApi.queryCommentsList(page, pagesize, movementId);

        // 获取当前页数据
        List<Comment> commentList = (List<Comment>) pageResult.getItems();

        // 2.封装返回结果
        List<CommentVo> voList = new ArrayList<>();

        if (commentList != null) {
            for (Comment comment : commentList) {
                // 2.1 创建vo
                CommentVo vo = new CommentVo();

                // 2.2 封装数据
                // 封装评论数据
                vo.setId(comment.getId().toString());
                vo.setContent(comment.getContent());
                vo.setCreateDate(new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date(comment.getCreated())));
                vo.setLikeCount(comment.getLikeCount());
                // 3.删除点赞标记
                String key = "comment_like_" + UserHolder.getUserId() + "_" + comment.getId();
                // 从Redis中删除Key
                if(redisTemplate.hasKey(key)){
                    vo.setHasLiked(1);
                }else {
                    vo.setHasLiked(0);
                }

                // 封装用户详情
                UserInfo userInfo = userInfoApi.findById(comment.getUserId());
                if (userInfo != null) {
                    vo.setAvatar(userInfo.getAvatar());
                    vo.setNickname(userInfo.getNickname());
                }

                // 2.3 添加vo到集合
                voList.add(vo);
            }
        }

        // 3.重新设置items的值
        pageResult.setItems(voList);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：评论-提交
     */
    public ResponseEntity<Object> saveComments(String movementId, String content) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());
        Publish byId = publishApi.findById(movementId);
        comment.setPublishUserId(byId.getUserId());

        // 2.调用服务提供者api保存评论、修改评论数量
        commentApi.save(comment);

        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞
     * @param videoId 视频ID
     * @return
     */
    public ResponseEntity<Object> videoLike(String videoId) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(videoId));// 设置视频的ID
        comment.setCommentType(1); // 设置评论类型为点赞
        comment.setPubType(2); // 设置对发布的动态是2-对视频操作
        comment.setUserId(UserHolder.getUserId()); // 设置用户的ID
        comment.setCreated(System.currentTimeMillis()); // 设置发表时间

/*
        // 根据动态id查询动态信息7
        Publish publish = publishApi.findById(videoId);
        // 设置动态发布者的id
        comment.setPublishUserId(publish.getUserId());
        */
        // 2.保存点赞记录 (评论 )、修改喜欢数量、返回喜欢数量
        long count = commentApi.saveVideoLike(comment);

        // 3.保存点赞标记到redis中
        String key = "video_like_comment_" + UserHolder.getUserId() + "_" + videoId;
        redisTemplate.opsForValue().set(key, "xxx");


        return ResponseEntity.ok(count);
    }

    /**
     * 视屏取消点赞
     * @param videoId 视屏取消点赞
     * @return
     */
    public ResponseEntity<Object> CancelVideoLikes(String videoId) {
        // 1.创建评论对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(videoId));
        comment.setCommentType(1); // 设置点赞操作
        comment.setPubType(2); // 设置对视屏的操作
        comment.setUserId(UserHolder.getUserId());

        // 2.调用服务提供者api删除评论、修改点赞数量、返回点赞数量
        long count = commentApi.deleteVideoLike(comment);

        // 3.删除点赞标记
        String key = "video_like_comment_" + UserHolder.getUserId() + "_" + videoId;
        // 从Redis中删除Key
        redisTemplate.delete(key);

        return ResponseEntity.ok(count);
    }

    //评论点赞
    public ResponseEntity<Object> likeComments(String id) {
        long count =commentApi.operationLikeConnents(id,1);

        // 3.保存点赞标记到redis中
        String key = "comment_like_" + UserHolder.getUserId() + "_" + id;
        redisTemplate.opsForValue().set(key, "xxx");

        return ResponseEntity.ok(count);
    }

    /**
     * 评论取消点赞
     * @param id 评论取消点赞
     * @return
     */
    public ResponseEntity<Object> unLikeComments(String id) {

        long count = commentApi.operationLikeConnents(id,-1);

        // 3.删除点赞标记
        String key = "comment_like_" + UserHolder.getUserId() + "_" + id;
        // 从Redis中删除Key
        redisTemplate.delete(key);

        return ResponseEntity.ok(count);
    }
}
